<template>
	<view class="container">
		<view v-for="(category,index) in tableData" :key="index">
			<view class="" >
				<!-- v-if="!dataFab || category.isShow" -->
				<uni-section :title="category.title" type="line">
					<view v-for="item in category.items" :key="item.index">
						<view class="" v-if="!dataFab || item.ishas"> 
							<uni-card :title="'标准项'+item.index" :extra="item.score.toString()">
								<text class="uni-body uni-mt-5">
									{{item.name}}
								</text>
								<view v-if="item.ishas" >
									<view class="" v-if="item.state !='2'" style="border-bottom: 1px solid #eceae5;">
										<uni-list-item  title="进度">
											<template v-slot:footer>
												<view class="slot-box">
													<uni-tag :text="item.state == '-1' ? '待提交' : item.state == '1'? '审核中':'审核通过' " :type="item.state == '-1' ? 'default' : item.state == '1'? 'primary':'success' " :circle="true" :inverted="true"></uni-tag>
												</view>
											</template>
										</uni-list-item>
									</view>
									<view class="" v-else style="border-bottom: 1px solid #eceae5;">
										<uni-list-item  :rightText="item.approvalPeople+':'+item.opinion">
											<template v-slot:header>
												<view class="slot-box">
													<uni-tag text="审核未通过" type="error" :circle="true" :inverted="true"></uni-tag>
												</view>
											</template>
										</uni-list-item>
									</view>
									
									<!-- <HR style="margin-top: -10px;" /> -->
									<p class="mytitle">请上传证明文件：</p>
									<button type="primary" size="mini" @tap="ChooseImage(index,item.index)">上传文件</button>
									<!-- <view class="grid col-4 grid-square flex-sub">
										<view class="bg-img" v-for="(item1,index1) in item.appendix" :key="index1" @tap="ViewImage(index,item.index,$event)" :data-url="item.appendix[index1]">
										 <image :src="imgUrl+item1" mode="aspectFill"></image>
											<view class="cu-tag bg-red radius" @tap.stop="DelImg(index,item.index,$event)" :data-index="index1">
												<text class='cuIcon-close'></text>
											</view>
										</view>
										<view class="solids" @tap="ChooseImage(index,item.index)" v-if="item.appendix.length<4">
											<text class='cuIcon-add'></text>
										</view>
									</view> -->
									<view class="filesitem" v-for="(item1,index1) in item.appendix" :key="index1":data-url="item.appendix[index1]">
										<view class="" style="width: 100%; height: 30px;">
											<uni-link :href="saveFile(item1)" :download="fileName(item1)">
												{{fileName(item1)}}
											</uni-link>
											<view class="" style="float: right;" @tap.stop="DelImg(index,item.index,$event)">
												<uni-icons  type="close" size="30" ></uni-icons>
											</view>
										</view>
									</view>
								</view>
								<view slot="actions" class="card-actions"
									style="display: flex;justify-content: center;align-items: center; ">
									<a v-if="item.ishas" class="myDeleteLink" @click="change(index,item.index)">
										<uni-icons color="#e43d33" type="clear" size="30"></uni-icons>
									</a>
									<a v-else class="myAddLink" @click="change(index,item.index)">
										<uni-icons color="#2979ff" type="plus-filled" size="30"></uni-icons>
									</a>
								</view>
							</uni-card>
						</view>
					</view>
					<br/>
				</uni-section>
			</view>
		</view>
		<br/>
		<button class="myfootbtn" type="primary" @click="upload()">提交审核</button>
		<br/>
	</view>
</template>

<script>
export default {
	name:"employeeScoreItem",
	props: {
		dataMsg: {
			type: Array,
			default: [
				{
					id: "1508751655557005314",
					title: "申请者居住状况计分",
					isShow: true,
					items: [
						{
							id:	"1508751655754137601",
							index: 1,
							score: 10,
							name: "夫妻双方均未享受过优惠政策住房",
							ishas: true,
							opinion: "11111",
							approvalPeople:"1111",
							approvalTime: "1111",
							note: null,
							ssiname: null,
							ssinote: null,
							ssiscore: null,
							state: "-1",
							appendix: [],
						},
						{
							id: "1508751655817052162",
							index: 2,
							score: 40,
							name: "夫妻双方均未享受过优惠政策住房,且夫妻双方（含双方未满18周岁的子女）均未在申请公租房所在地购买或自建过住房",
							ishas: true,
							opinion: null,
							approvalPeople:null,
							approvalTime: null,
							note: null,
							ssiname: null,
							ssinote: null,
							ssiscore: null,
							state: "-1",
							appendix: [],
						}
					],
				},
			],
		},
		dataFab: {
			type: Boolean,
			default:false
		},
		dataSum: {
			type: Number,
			dafault: 50,
		},
		delList: {
			type: Array,
		}
	},
	data() {
		return {
			imgUrl: this.$config.staticDomainURL+'/',
			uploadUrl:"/sys/common/upload",
			tableData: this.dataMsg,
			// [
			// 	{
			// 		id: "1508751655557005314",
			// 		title: "申请者居住状况计分",
			// 		isShow: true,
			// 		items: [
			// 			{
			// 				id:	"1508751655754137601",
			// 				index: 1,
			// 				score: 10,
			// 				name: "夫妻双方均未享受过优惠政策住房",
			// 				ishas: true,
			// 				isRead: true,
			// 				opinion: "11111",
			// 				approvalPeople:"1111",
			// 				approvalTime: "1111",
			// 				note: null,
			// 				ssiname: null,
			// 				ssinote: null,
			// 				ssiscore: null,
			// 				appendix: [],
			// 			},
			// 			{
			// 				id: "1508751655817052162",
			// 				index: 2,
			// 				score: 40,
			// 				name: "夫妻双方均未享受过优惠政策住房,且夫妻双方（含双方未满18周岁的子女）均未在申请公租房所在地购买或自建过住房",
			// 				ishas: true,
			// 				isRead: false,
			// 				opinion: null,
			// 				approvalPeople:null,
			// 				approvalTime: null,
			// 				note: null,
			// 				ssiname: null,
			// 				ssinote: null,
			// 				ssiscore: null,
			// 				appendix: [],
			// 			}
			// 		],
			// 	},
			// ],
			isInsert: this.dataFab,
			sum: 50,
			deleteList: this.delList,
		}
	},
	watch:{
	    delList(n,o){ //n为新值,o为旧值;
	      this.deleteList = n
	    },
		dataMsg(n,o){
			this.tableData = n
		}
	},
	mounted() {
		this.$http.post(`/base/znzfEmployeeScoreItem/queryLatestScoreByUserId?userId=${this.$store.getters.userid}`).then(res => {
			// console.log(res.data.result)
			this.sum = res.data.result
		})
		console.log(this.tableData,this.sum,this.deleteList)
		// console.log(this.tableData[0].items[0].opinion == null)
	},
	methods: {
		saveFile(url) {
			let totalUrl = this.imgUrl+url;
			// console.log(totalUrl)
			return totalUrl
		},
		fileName(e) {
			let temp = e.split(".")
			let suffix = temp.pop()
			temp = temp.join(".")
			temp = temp.split("_")
			temp.pop()
			let name = temp.join("_")
			// console.log(name+"."+suffix)
			return name+"."+suffix
		},
		onClick(e){
			console.log(e)
		},
		actionsClick(text){
			uni.showToast({
				title:text,
				icon:'none'
			})
		},
		change(index,index1){
			// console.log(index,index1)
			this.tableData[index].items[index1-1].ishas = !this.tableData[index].items[index1-1].ishas
			// this.ishas = !this.ishas;
			if (this.tableData[index].items[index1-1].ishas) {
				uni.showToast({
					title:"目前分数：" + (this.sum+=this.tableData[index].items[index1-1].score),
					icon:'none'
				})
			}else {
				uni.showToast({
					title:"目前分数：" + (this.sum-=this.tableData[index].items[index1-1].score),
					icon:'none'
				})
			}
			this.$emit('childSum', {sum:this.sum,flag:false})
			console.log(this.sum)
		},
		ChooseImage(index,index1) {
			// console.log(this.tableData[index].items[index1-1])
			var that=this;
			if (!that.tableData[index].items[index1-1].appendix) {
				that.tableData[index].items[index1-1].appendix = []
			}
			uni.chooseFile({
				count: 4, //默认100
				type: 'all',
				success: (res) => {
					console.log("111111",res.tempFilePaths)
					res.tempFilePaths.forEach(item => {
						that.$http.upload(that.$config.apiUrl+that.uploadUrl, {
							filePath: item,
							name: 'file'
						})
						.then(res => {
							console.log(res)
							that.tableData[index].items[index1-1].appendix.push(res.data.message)
							console.log(that.tableData[index].items[index1-1].appendix)
						})
						.catch(err => {
							that.$tip.error(err.data.message)
						});
					})
				}
			});
		},
		// index,index1,数组下标
		ViewImage(index,index1,e) {
			console.log(index,index1,e)
			let temp = []
			this.tableData[index].items[index1-1].appendix.forEach(item => temp.push(this.$config.staticDomainURL+'/'+item))
			// this.tableData[index].appendix.forEach(item => temp.push(this.$config.staticDomainURL+'/'+item))
			console.log(temp)
			uni.previewImage({
				urls: temp,
				current: e.currentTarget.dataset.url
			});
		},
		DelImg(index,index1,e) {
			console.log(index,index1,e)
			uni.showModal({
				// title: this.person.name,
				content: '确定要删除吗？',
				cancelText: '取消',
				confirmText: '确定',
				success: res => {
					if (res.confirm) {
						console.log(1,e.currentTarget.dataset.index)
						this.tableData[index].items[index1-1].appendix.splice(e.currentTarget.dataset.index, 1)
					}
				}
			})
		},
		upload(e) {
			let	znzfEmployeeScoreItemParamList=[]
			this.sum = 0
			console.log(this.tableData)
			this.tableData.forEach(category => {
				category.items.forEach(item => {
					if (item.ishas) {
						let temp = {
							userId: "",
							scoreItemId: "",
							score: "",
							appendix: "",
							opinion: null,
							approvalPeople:null,
							approvalTime: null,
							note: null,
							// ssiname: null,
							// ssinote: null,
							// ssiscore: null,
							state: "1",
						}
						temp.state = item.state
						temp.userId = this.$store.getters.userid
						temp.scoreItemId = item.id
						temp.score = item.score
						temp.opinion = item.opinion
						temp.approvalPeople = item.approvalPeople
						temp.approvalTime = item.approvalTime
						temp.note = item.note
						// temp.ssiname = item.ssiname
						// temp.ssinote = item.ssinote
						// temp.ssiscore = item.ssiscore
						this.sum += temp.score
						console.log(temp.state)
						if (item.appendix.length) {
							// console.log(temp.score,item.appendix)
							temp.appendix = item.appendix.join(",")
						}else temp.appendix = ""
						znzfEmployeeScoreItemParamList.push(temp)
					}
				})
			})
			console.log(znzfEmployeeScoreItemParamList)
			console.log(this.deleteList)
			let del = []
			this.deleteList.forEach(item =>{del.push(item.id)})
			let delStr = del.join()
			console.log(delStr)
			this.$http.delete(`/base/znzfEmployeeScoreItem/deleteBatch?ids=${delStr}`).then(res => {
				console.log(res)
				this.$http.post('/base/znzfEmployeeScoreItem/insertEmployeeScoreItemList',{znzfEmployeeScoreItemParamList}).then(res => {
					console.log(res)
					console.log(this.sum.toString(),typeof this.sum.toString())
					this.$http.post(`/base/znzfEmployeeScoreItem/insertLatestScoreByUserId?userId=${this.$store.getters.userid}&latestScore=${this.sum.toString()}`).then(res => {
						console.log(res)
					})
					this.$emit('childSum', {sum:this.sum,flag:true})
					uni.showToast({
						title: "已提交审核",
						icon: 'none'
					})
				})
			})
		},
	}
}
</script>

<style lang='scss'>
	.mytitle{
		color: #3f3f3f;
		font-size: 16px;
		margin: 5px 0;
	}
	.filesitem{
		color: #636363;
		font-size: 16px;
		margin: 2px 0;
	}
	.myAddLink{
		text-align: center;
		color: #2979ff;
		font-size: 20px;
		margin: 2px 0;
	}
	.myDeleteLink{
		text-align: center;
		color: #e43d33;
		font-size: 20px;
		margin: 2px 0;
	}
	.myfootbtn{
		margin: 8px 5%;
		width: 90%;
	}
</style>
